//
//  FMCommentTableViewCell.m
//  PsychologyFM
//
//  Created by zhang on 16/1/13.
//  Copyright © 2016年 ZhangLuDu. All rights reserved.
//

#import <UIButton+WebCache.h>
#import <UIImageView+WebCache.h>
#import "FMCommentTableViewCell.h"

@implementation FMCommentTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self addSubview];
    }
    return self;
}
- (void)addSubview{
    
//    头像
    self.avatarButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.avatarButton.frame = CGRectMake(kMartin10, kMartin10, kScreenWidth/10, kScreenWidth/10);
    self.avatarButton.backgroundColor = FMGreen;
    [self.contentView addSubview:_avatarButton];
//    楼层昵称
    self.nicknameButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.nicknameButton.frame = CGRectMake(self.avatarButton.right + kMartin5, self.avatarButton.top + kMartin5, kMartin25 * 4, self.avatarButton.height/2) ;
    [self.nicknameButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    [self.nicknameButton setTitleColor:[UIColor colorWithRed:145/255.0 green:145/255.0 blue:145/255.0 alpha:1] forState:(UIControlStateNormal)];
    [self.contentView addSubview:_nicknameButton];
    
    
//    回复内容
    self.contentLable = [[UILabel alloc]initWithFrame:CGRectMake(self.nicknameButton.left, self.avatarButton.bottom, kScreenWidth - kMartin15 - self.nicknameButton.left, 20)];
    self.contentLable.font = [UIFont systemFontOfSize:14];
    self.contentLable.numberOfLines = -1;
    [self.contentView addSubview:_contentLable];
    
    
//    创建日期
    self.createdLable = [[UILabel alloc]initWithFrame:CGRectMake(self.contentLable.left, self.contentLable.bottom + kMartin15, kMartin20 * 5, kMartin15)];
    self.createdLable.font = [UIFont systemFontOfSize:14];
    self.createdLable.textColor = [UIColor colorWithRed:145/255.0 green:145/255.0 blue:145/255.0 alpha:1] ;
    [self.contentView addSubview:_createdLable];
//    回复按钮
    self.repeatButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [self.repeatButton setImage:[UIImage imageNamed:@"commentButton"] forState:(UIControlStateNormal)];
    [self.contentView addSubview:_repeatButton];
    
}
#pragma mark 重写model的set方法
- (void)setModel:(FMCommentModel *)model{

    _model = model;
    [self.avatarButton sd_setBackgroundImageWithURL:[NSURL URLWithString:model.user[@"avatar"]] forState:(UIControlStateNormal)];
    [self.nicknameButton setTitle:[NSString stringWithFormat:@"%@楼 %@",model.floor,model.user[@"nickname"]] forState:(UIControlStateNormal)];
    self.nicknameButton.titleLabel.font = [UIFont systemFontOfSize:14];
    self.createdLable.text = model.created;
    
    //点击回复按钮 弹出的imageView

    self.repeatView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];


    [self.contentView addSubview:_repeatView];

#pragma mark 自定义contentLable的高度
    
    //构建字体大小的字典
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:14], NSFontAttributeName ,nil];
    
    CGRect frame = [model.content boundingRectWithSize:CGSizeMake(kScreenWidth - kScreenWidth/10 - kMartin25,CGFLOAT_MAX ) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    self.contentLable.frame = CGRectMake(self.nicknameButton.left, self.avatarButton.bottom , kScreenWidth - kScreenWidth/10 - kMartin25, frame.size.height );
    
 
    
    self.contentLable.text = model.content;
    
    self.createdLable.frame = CGRectMake(self.contentLable.left, self.contentLable.bottom + kMartin15, kMartin20 * 5, kMartin15);
    self.repeatButton.frame = CGRectMake(kScreenWidth - 50, self.createdLable.top - kMartin5, kMartin25, self.createdLable.height + kMartin10);
    self.repeatView.frame = CGRectMake(self.repeatButton.left - kMartin20 * 5, self.repeatButton.center.y - kMartin20,kMartin20 * 6 , kMartin20 * 2);
    //点击回复按钮 弹出的imageView
    [self.repeatView setImage:[UIImage imageNamed:@"menu111@2x"]];
    self.repeatView.hidden = YES;
    self.repeatView.userInteractionEnabled = YES;
}

- (void)awakeFromNib {
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
